const express = require('express');

const router = express.Router();

const resultUtils = require('../../utils/result_utils');

const whiteList = ['/news/list', '/news/find', '/news/release', '/news/findInfo', '/admin/login', '/common/stockInfo', '/feature/list'];

function checkUrl(path) {
  let isAccess = true;
  whiteList.forEach(value => {
    if (path.indexOf(value) !== -1) {
      isAccess = true;
    }
  });
  return isAccess;
}

router.use('/', (req, res, next) => {
  // req.url !== '/admin/login' && !req.session.user &&
  if (checkUrl(req.path) || req.session.user) {
    next();
  } else {
    res.send(resultUtils.createErrorResponse(resultUtils.ErrorCode.ERROR_CODE_NO_PERMISSION));
  }
});

router.use('/admin', require('./admin'));

module.exports = router;
